<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="模板名称" prop="templateName">
            <el-input v-model="queryParams.templateName" placeholder="请输入模板名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table v-loading="loading" :data="smsLogList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <!-- <el-table-column label="编号" align="center" prop="index" v-if="true" /> -->
        <el-table-column label="模板名称" align="center" prop="templateName" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="短信内容" align="center" prop="content" />
        <el-table-column label="发送状态" align="center" prop="sendStatus">
          <template #default="scope">
            <dict-tag :options="send_status_list" :value="scope.row.sendStatus" type="success" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="接收状态" align="center" prop="receiveStatus" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="日志详情" placement="top">
              <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['system:smsLog:detail']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改邮件日志对话框 -->
    <el-dialog title="邮件日志详细" v-model="dialog.visible" width="700px" append-to-body>
      <el-form :model="form" label-width="100px" ref="smsLogList">
        <el-row>
          <el-col :span="12">
            <el-form-item label="模板名称：">{{ form.templateName }}</el-form-item>
            <el-form-item label=" 手机号：">{{ form.phone }}</el-form-item>
            <el-form-item label="短信内容:">{{ form.content }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="请求日志：">{{ form.requestLog }}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发送状态:">
              <div v-if="form.sendStatus === 0">成功</div>
              <div v-else-if="form.sendStatus === 1">失败</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialog.visible = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SmsLog" lang="ts">
import { listSmsLog, getSmsLog, delSmsLog, addSmsLog, updateSmsLog } from '@/api/sms/log/index';
import { SmsLogVO, SmsLogQuery, SmsLogForm } from '@/api/sms/log/types';
import { ComponentInternalInstance,defineComponent, ref, Ref } from 'vue';
import { ElForm } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const smsLogList = ref<SmsLogVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref(ElForm);
const smsLogFormRef = ref(ElForm);

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

interface Option {
label: string;
value: string;
};

  const send_status_list: Ref<Option[]> = ref([
    { label: '成功', value: '0'},
    { label: '失败', value: '1' },
  ]);



const initFormData: SmsLogForm = {
  id: '',
  templateId: '',
  phone: '',
  content: '',
  sendStatus: '',
  receiveStatus: '',
  templateName: '',
  requestLog:''
}
const data = reactive<PageData<SmsLogForm, SmsLogQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    templateId: '',
    phone: '',
    content: '',
    sendStatus: '',
    receiveStatus: '',
    templateName: ''
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    templateId: [
      { required: true, message: "模板ID不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "手机号不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "短信内容不能为空", trigger: "blur" }
    ],
    sendStatus: [
      { required: true, message: "发送状态不能为空", trigger: "change" }
    ],
    receiveStatus: [
      { required: true, message: "接收状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询邮件日志列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSmsLog(queryParams.value);
  smsLogList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  smsLogFormRef.value.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: SmsLogVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleDetail = (row?: SmsLogVO) => {
  dialog.visible = true;
  dialog.title = "邮件日志详情";
  form.value = row;
}

/** 修改按钮操作 */
const handleUpdate = (row?: SmsLogVO) => {
  loading.value = true
  dialog.visible = true;
  dialog.title = "修改邮件日志";
  nextTick(async () => {
    reset();
    const _id = row?.id || ids.value[0]
    const res = await getSmsLog(_id);
    loading.value = false;
    Object.assign(form.value, res.data);
  });
}

/** 提交按钮 */
const submitForm = () => {
  smsLogFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSmsLog(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addSmsLog(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: SmsLogVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除邮件日志编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delSmsLog(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/smsLog/export', {
    ...queryParams.value
  }, `smsLog_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
